package com.study.webapp.word;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

public class WordToXml {

	public static String inputFile = "D:\\wordlib\\aa.doc";

	private static String filePath = "D:/wordlib/abd.xml";

	public static void main(String[] args) {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(inputFile);
			out = new FileOutputStream(filePath);
			new WordToXml().wordToXml(in, out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private Writer writer = null;

	public void wordToXml(InputStream in, OutputStream out) {
		HWPFDocument doc = null;
		try {
			doc = new HWPFDocument(in);
			writer = new OutputStreamWriter(out, Charset.forName("UTF-8"));
			init();
			openDocument();
			openBody();
			Range r = doc.getRange();
			StyleSheet styleSheet = doc.getStyleSheet();
			int sectionLevel = 0;
			int lenParagraph = r.numParagraphs();
			boolean inCode = false;
			for (int i = 0; i < lenParagraph; i++) {
				Paragraph p = r.getParagraph(i);
				String text = p.text();
				if (text.trim().length() == 0) {
					continue;
				}
				StyleDescription pStyle = styleSheet.getStyleDescription(p.getStyleIndex());
				String styleName = pStyle.getName();
				if (styleName.startsWith("Heading")) {
					if (inCode) {
						closeSource();
						inCode = false;
					}
					int headerLevel = Integer.parseInt(styleName.substring(8));
					if (headerLevel > sectionLevel) {
						openSection();
					} else {
						for (int j = 0; j < (sectionLevel - headerLevel) + 1; j++) {
							closeSection();
						}
						openSection();
					}
					sectionLevel = headerLevel;
					openTitle();
					System.out.println("---------" + p.text());
					writerPlainText(text);
					closeTitle();

				} else {
					int cruns = p.numCharacterRuns();
					CharacterRun run = p.getCharacterRun(0);
					String fontName = run.getFontName();
					if (fontName.startsWith("Courier")) {
						if (!inCode) {
							openSource();
							inCode = true;
						}
						System.out.println("---------" + p.text());
						writerPlainText(p.text());
					} else {
						if (inCode) {
							inCode = false;
							closeSource();
						}
						openParagraph();
						System.out.println("*******" + p.text());
						writerPlainText(p.text());
						closeParagraph();
					}

				}
			}
			for (int x = 0; x < sectionLevel; x++) {
				closeSection();
			}
			closeBody();
			closeDocument();
			writer.flush();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public void init() throws Exception {
		writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
		writer.write(
				"<!DOCTYPE document PUBLIC \"-//APACHE//DTD Documentation V1.1//EN\" \"./dtd/document-v11.dtd\">\r\n");
	}

	public void openDocument() throws Exception {
		writer.write("<document>\r\n");
	}

	public void closeDocument() throws Exception {
		writer.write("</document>\r\n");
	}

	public void openBody() throws Exception {
		writer.write("<body>\r\n");
	}

	public void closeBody() throws Exception {
		writer.write("</body>\r\n");
	}

	public void openSection() throws Exception {
		writer.write("<section>\r\n");
	}

	public void closeSection() throws Exception {
		writer.write("</section>\r\n");
	}

	public void openTitle() throws Exception {
		writer.write("<title>\r\n");
	}

	public void closeTitle() throws Exception {
		writer.write("</title>\r\n");
	}

	public void writerPlainText(String text) throws Exception {
		writer.write(text);
	}

	public void openParagraph() throws Exception {
		writer.write("<p>");
	}

	public void closeParagraph() throws Exception {
		writer.write("</p>");
	}

	public void openSource() throws Exception {
		writer.write("<source><![CDATA[");
	}

	public void closeSource() throws Exception {
		writer.write("]]></source>");
	}

}
